<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class MY_Model extends CI_Model
{
    protected $_db;
    protected $_table;
    function __construct()
    {
        $this->_db = &$this->pdo;
        $sub_class = strtolower( get_called_class() );
        $postfix = '_model';
        if (strrchr( $sub_class, $postfix ) == $postfix)
        {
            $sub_class = str_replace( $postfix, '', $sub_class );
        }
        $this->_table = $sub_class;
        parent::__construct();
    }
    function insert($data)
    {
        $this->_db->insert( $this->_table, $data );
        return $this->_db->last_id();
    }
    function update($data, $id, $id_field = '`id`')
    {
        $this->_db->update( $this->_table, $data, $id_field . '=?', $id );
        return $this->_db->affected_rows();
    }
    function bulk_update($data, $ids, $id_field = '`id`')
    {
        $this->_db->update( $this->_table, $data, $id_field . ' in(?)', $ids );
        return $this->_db->affected_rows();
    }
    function delete($id, $id_field = '`id`')
    {
        $this->_db->delete( $this->_table, $id_field . '=?', $id );
        return $this->_db->affected_rows();
    }
}